﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class LeaseContract
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}{1}", ContractNumber, IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        public IList<LeaseContractReceipt> LeaseContractReceipts
        {
            get
            {
                return InternalLeaseContractReceipts.Where(leaseContractReceipt => leaseContractReceipt.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Add & Remove LeaseContractReceipt

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public LeaseContractReceipt AddLeaseContractReceipt(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var leaseContractReceipt = new LeaseContractReceipt
                                           {
                                               RowId = Guid.NewGuid(),
                                               CreatedDate = DateTime.Now,
                                           };
            InternalLeaseContractReceipts.Add(leaseContractReceipt);
            leaseContractReceipt.IsDeleted = false;
            leaseContractReceipt.ModifiedDate = DateTime.Now;
            leaseContractReceipt.ModifiedByUser = modifiedUser;
            leaseContractReceipt.ReceiptTypeId = 1;
            leaseContractReceipt.ScheduledDate = DateTime.Now.Date.AddDays(7);
            leaseContractReceipt.ReceivableAmount = 0;
            leaseContractReceipt.ReceivedAmount = 0;
            return leaseContractReceipt;
        }

        /// <summary>
        /// </summary>
        /// <param name = "receiptType"></param>
        /// <param name = "scheduledDate"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public LeaseContractReceipt AddLeaseContractReceipt(ReceiptType receiptType, DateTime scheduledDate, User modifiedUser)
        {
            if (receiptType == null)
            {
                throw new ArgumentNullException("receiptType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalLeaseContractReceipts.Any(item => item.IsDeleted == false && item.ReceiptType == receiptType && item.ScheduledDate == scheduledDate.Date))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedLeaseContractReceipt, receiptType.Name, scheduledDate.Date.ToString("yyyy年MM月dd日")));
            }
            var leaseContractReceipt = InternalLeaseContractReceipts.FirstOrDefault(item => item.IsDeleted && item.ReceiptType == receiptType && item.ScheduledDate == scheduledDate.Date);
            if (leaseContractReceipt == null)
            {
                leaseContractReceipt = new LeaseContractReceipt
                                           {
                                               RowId = Guid.NewGuid(),
                                               CreatedDate = DateTime.Now,
                                           };
                InternalLeaseContractReceipts.Add(leaseContractReceipt);
            }
            leaseContractReceipt.IsDeleted = false;
            leaseContractReceipt.ModifiedDate = DateTime.Now;
            leaseContractReceipt.ModifiedByUser = modifiedUser;
            leaseContractReceipt.ReceiptType = receiptType;
            leaseContractReceipt.ScheduledDate = scheduledDate.Date;
            leaseContractReceipt.ReceivableAmount = 0;
            leaseContractReceipt.ReceivedAmount = 0;
            return leaseContractReceipt;
        }

        /// <summary>
        /// </summary>
        /// <param name = "leaseContractReceipt"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveLeaseContractReceipt(LeaseContractReceipt leaseContractReceipt, User modifiedUser)
        {
            if (leaseContractReceipt == null)
            {
                throw new ArgumentNullException("leaseContractReceipt");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalLeaseContractReceipts.Contains(leaseContractReceipt))
            {
                leaseContractReceipt.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(ContractNumber))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "LeaseContract", "ContractNumber"));
            }
            if (ReducedUnitPricePerMonth < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContract", "ReducedUnitPricePerMonth"));
            }
            if (ReducedTotalPricePerMonth < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContract", "ReducedTotalPricePerMonth"));
            }
            if (LeaseMonths < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContract", "LeaseMonths"));
            }
            if (TotalAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContract", "TotalAmount"));
            }
            if (LeaseAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContract", "LeaseAmount"));
            }
            if (GuarantyAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContract", "GuarantyAmount"));
            }
            if (OtherAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContract", "OtherAmount"));
            }
            if (string.IsNullOrEmpty(ContractNumber))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "LeaseContract", "ContractNumber"));
            }
            ContractNumber = ContractNumber.Length > 32 ? ContractNumber.Substring(0, 32) : ContractNumber;
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<LeaseContractReceipt>) LeaseContractReceipts).ForEach(leaseContractReceipt => leaseContractReceipt.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}